package com.zyd.work.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zyd.common.AjaxUtils;
import com.zyd.common.CommonController;
import com.zyd.work.domain.vo.WeekLog;
import com.zyd.work.logic.IWeekLogFacade;
import com.zyd.xtgl.domain.vo.TbAdmUser;

@Controller
@RequestMapping("/weeklog")
public class WeekLogAction extends CommonController {
	private IWeekLogFacade weekLogImpl;

	@Resource(name = "weekLogImpl")
	public void setWeekLogImpl(IWeekLogFacade weekLogImpl) {
		this.weekLogImpl = weekLogImpl;
	}

	/**
	 * 转向到周报主页面
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=toWeekLog")
	public ModelAndView toWeekLog(HttpServletRequest request,
			HttpServletResponse response) {
		return new ModelAndView("worklog/myweeklog");
	}

	/**
	 * 添加周报
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=addWeekLog")
	public String addWeekLog(HttpServletRequest request,
			HttpServletResponse response, @Valid WeekLog weeklog,
			BindingResult result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		TbAdmUser user = (TbAdmUser) request.getSession().getAttribute("user");
		List<WeekLog> list = weekLogImpl.getWeekLogByUserid(weeklog.getWeekofyear(),
				user.getUserid());
		if (result.hasErrors()) {
			out.write("输入有误,添加周报失败~~~~");
		} else if (list.size() > 0) {
			out.write("您已经添加本周周报，不能重复添加~~~~");
		} else {
			weeklog.setUserid(user.getUserid());
			weekLogImpl.addWeekLog(weeklog);
			out.write("添加周报成功~~~~");
		}
		out.close();
		return null;
	}

	/**
	 * 更新周报
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=updateWeekLog")
	public String updateWeekLog(HttpServletRequest request,
			HttpServletResponse response, @Valid WeekLog weeklog,
			BindingResult result) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		TbAdmUser user = (TbAdmUser) request.getSession().getAttribute("user");
		List<WeekLog> list = weekLogImpl.getWeekLogByUserid(weeklog.getWeekofyear(),
				user.getUserid());
		if (result.hasErrors()) {
			out.write("信息有误,更新周报失败~~~~");
		}else if (list.size() > 0) {
			out.write("您已经添加本周周报，不能重复添加~~~~");
		}  else {
			weekLogImpl.updateWeekLog(weeklog);
			out.write("更新周报成功~~~~");
		}
		out.close();
		return null;
	}

	/**
	 * 根据id获取周报信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getWeekLogByid")
	public String getWeekLogByid(HttpServletRequest request,
			HttpServletResponse response, String id) {
		WeekLog weeklog = weekLogImpl.getWeekLog(id);
		return AjaxUtils.ajaxList(request, response, weeklog);
	}

	/**
	 * 单独提交周报的方法。传的是提交状态
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 */
	@RequestMapping(params = "method=commitWeekLog")
	public String commitWeekLog(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		String id = request.getParameter("id");
		String iscommit = request.getParameter("iscommit");
		WeekLog weeklog = weekLogImpl.getWeekLog(id);
		weeklog.setIscommit(iscommit);
		weekLogImpl.updateWeekLog(weeklog);
		out.write("提交成功~~~~");
		out.close();
		return null;
	}

	/**
	 * 登录用户查询自己的周报，通过开始周，和接受周查询。
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=searchWeekLogList")
	public String searchWeekLogList(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String startweek = request.getParameter("startweek");
		String endweek = request.getParameter("endweek");
		TbAdmUser user = (TbAdmUser) request.getSession().getAttribute("user");
		List<WeekLog> list = weekLogImpl.searchWeekLogByuserid(
				Integer.parseInt(startweek), Integer.parseInt(endweek),
				user.getUserid());
		return AjaxUtils.ajaxList(request, response, list);
	}

	/**
	 * 根据周次获取周报信息
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(params = "method=getNowDateWeekLogList")
	public String getNowDateWeekLogList(HttpServletRequest request,
			HttpServletResponse response) {
		TbAdmUser user = (TbAdmUser) request.getSession().getAttribute("user");
		String nowweek = request.getParameter("nowweek");
		int weekofyear = Integer.parseInt(nowweek);
		List<WeekLog> list = weekLogImpl.getWeekLogByUserid(weekofyear,
				user.getUserid());
		return AjaxUtils.ajaxList(request, response, list);
	}

}
